﻿package  {
	import flash.display.AVM1Movie;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.net.LocalConnection;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Tree extends Sprite{
		//private var swf:MovieClip;
		// local connection instance to communicate to AVM1 movie
		var AVM_lc:LocalConnection = new LocalConnection();
		var crashed:Boolean;
		
		public function Tree(posX:Number, posY:Number) {
			// constructor code
			this.x = posX - 27; // WIDTH/2
			this.y = posY - 77; // HEIGHT
			this.crashed = false;

			var req:URLRequest = new URLRequest("tree.swf");
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, init);
			loader.load(req);
			addChild(loader);
			
			// DEBUG : DONE!
			//loader.addEventListener(MouseEvent.CLICK, cras);
			//loader.addEventListener(MouseEvent.CLICK, windM);
		}
		
		private function init(loadEvent:Event) {
			//var mov:AVM1Movie = loadEvent.currentTarget.content;	
		
			/*** WHEN SWF IS NEW */
			//this.swf = loadEvent.currentTarget.content;
			//addChild(swf);
			//trace(swf.height + "||" + swf.totalFrames);
			//swf.gotoAndStop("1"); //TODO; it doesn't work but don't know why
		
		}
		//private function cras(e:Event) {crash()} //DEBUG
		public function crash():void {
			// Play CRASH tree animation
			if(!crashed) {
				AVM_lc.send("tree", "crash");
				this.crashed = true;
			}
		}
		//private function windM(e:Event) {windMove()} //DEBUG
		public function windMove():void {
			//Play WIND animation
			if(!crashed) { 
				AVM_lc.send("tree", "windMove");
			}
		}
	}
	
}
